
package com.shop.cloud.mall.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cloud.common.core.constant.CacheConstants;
import com.shop.cloud.common.core.constant.CommonConstants;
import com.shop.cloud.common.data.tenant.TenantContextHolder;
import com.shop.cloud.mall.common.entity.GoodsCategoryShop;
import com.shop.cloud.mall.admin.mapper.GoodsCategoryShopMapper;
import com.shop.cloud.mall.admin.service.GoodsCategoryShopService;
import com.shop.cloud.mall.common.entity.GoodsCategoryShopTree;
import com.shop.cloud.upms.common.util.TreeUtil;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 店铺商品分类表
 *
 * @author
 * @date 2020-08-26 08:58:11
 */
@Service
@AllArgsConstructor
public class GoodsCategoryShopServiceImpl extends ServiceImpl<GoodsCategoryShopMapper, GoodsCategoryShop> implements GoodsCategoryShopService {

	private final RedisTemplate redisTemplate;

	@Override
	public List<GoodsCategoryShopTree> selectTree(GoodsCategoryShop goodsCategoryShop) {
		return getTree(this.list(Wrappers.lambdaQuery(goodsCategoryShop)));
	}

	/**
	 * 构建树
	 *
	 * @param entitys
	 * @return
	 */
	private List<GoodsCategoryShopTree> getTree(List<GoodsCategoryShop> entitys) {
		List<GoodsCategoryShopTree> treeList = entitys.stream()
				.filter(entity -> !entity.getId().equals(entity.getParentId()))
				.sorted(Comparator.comparingInt(GoodsCategoryShop::getSort))
				.map(entity -> {
					GoodsCategoryShopTree node = new GoodsCategoryShopTree();
					BeanUtil.copyProperties(entity,node);
					return node;
				}).collect(Collectors.toList());
		return TreeUtil.build(treeList, CommonConstants.PARENT_ID);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean save(GoodsCategoryShop entity) {
		super.save(entity);
		//清缓存
		redisTemplate.delete(getRedisKey(entity));
		return true;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean removeById(Serializable id) {
		//清缓存
		GoodsCategoryShop entity = baseMapper.selectById(id);
		if(BeanUtil.isNotEmpty(entity)){
			redisTemplate.delete(getRedisKey(entity));
		}
		super.removeById(id);
		return true;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateById(GoodsCategoryShop entity) {
		super.updateById(entity);
		//清缓存
		entity = baseMapper.selectById(entity.getId());
		if(BeanUtil.isNotEmpty(entity)){
			redisTemplate.delete(getRedisKey(entity));
		}
		return true;
	}

	String getRedisKey(GoodsCategoryShop goodsCategoryShop){
		return StrUtil.format("{}:{}::{}", TenantContextHolder.getTenantId(), CacheConstants.SHOP_GOODSCATEGORY_CACHE, goodsCategoryShop.getShopId());
	}
}
